<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>vue钩子函数 | 欢迎来到guo的博客!</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="vue的生命周期vue的钩子函数是跟vue的生命周期有着密切关系的，而vue的1.0和2.0版本的变化也导致了vue钩子函数命名有了很大的变化。 根据提供的生命周期钩子说明Vue.js实例各个阶段的情况，Vue.js 2.0对不少钩子进行了修改，以下说明：    vue1.0 vue2.0    init ： 在实例开始初始化时同步调用，此时数据观测、时间等都尚未初始化。 2.0中更名为 bef">
<meta property="og:type" content="article">
<meta property="og:title" content="vue钩子函数">
<meta property="og:url" content="http://shangguan-lingfeng.gitee.io/blog1/2021/01/26/vue%E9%92%A9%E5%AD%90%E5%87%BD%E6%95%B0/index.html">
<meta property="og:site_name" content="欢迎来到guo的博客!">
<meta property="og:description" content="vue的生命周期vue的钩子函数是跟vue的生命周期有着密切关系的，而vue的1.0和2.0版本的变化也导致了vue钩子函数命名有了很大的变化。 根据提供的生命周期钩子说明Vue.js实例各个阶段的情况，Vue.js 2.0对不少钩子进行了修改，以下说明：    vue1.0 vue2.0    init ： 在实例开始初始化时同步调用，此时数据观测、时间等都尚未初始化。 2.0中更名为 bef">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-01-25T16:00:00.000Z">
<meta property="article:modified_time" content="2021-04-02T03:42:50.069Z">
<meta property="article:author" content="Jerry">
<meta property="article:tag" content="vue">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/blog1/atom.xml" title="欢迎来到guo的博客!" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/blog1/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/blog1/css/style.css">

  
    
<link rel="stylesheet" href="/blog1/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/blog1/" id="logo">欢迎来到guo的博客!</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/blog1/">Home</a>
        
          <a class="main-nav-link" href="/blog1/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/blog1/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Suche"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Suche"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://shangguan-lingfeng.gitee.io/blog1"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-vue钩子函数" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/blog1/2021/01/26/vue%E9%92%A9%E5%AD%90%E5%87%BD%E6%95%B0/" class="article-date">
  <time class="dt-published" datetime="2021-01-25T16:00:00.000Z" itemprop="datePublished">2021-01-26</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      vue钩子函数
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <hr>
<h3 id="vue的生命周期"><a href="#vue的生命周期" class="headerlink" title="vue的生命周期"></a>vue的生命周期</h3><p>vue的钩子函数是跟vue的生命周期有着密切关系的，而vue的1.0和2.0版本的变化也导致了vue钩子函数命名<br>有了很大的变化。</p>
<p><strong>根据提供的生命周期钩子说明Vue.js实例各个阶段的情况，Vue.js 2.0对不少钩子进行了修改，以下说明：</strong></p>
<table>
<thead>
<tr>
<th align="left">vue1.0</th>
<th align="left">vue2.0</th>
</tr>
</thead>
<tbody><tr>
<td align="left">init ： 在实例开始初始化时同步调用，此时数据观测、时间等都尚未初始化。</td>
<td align="left">2.0中更名为 <strong>beforeCreate</strong>。</td>
</tr>
<tr>
<td align="left"><strong>created</strong>：在实例创建之后调用，此时已经完成数据绑定、事件方法，但尚未开始 DOM 编译，即未挂载到 document 中。</td>
<td align="left">无新增特性</td>
</tr>
<tr>
<td align="left">无</td>
<td align="left"><strong>beforeMount</strong>：2.0 新增的生命周期钩子，在 mounted 之前运行。</td>
</tr>
<tr>
<td align="left">compiled：在编译结束时调用。此时所有指令已生效</td>
<td align="left">2.0 中更名为 <strong>mounted</strong></td>
</tr>
<tr>
<td align="left">无</td>
<td align="left"><strong>beforeUpdate</strong>：2.0 新增的生命周期钩子，在实例挂载会后，再次更新实例时会调用该方法。</td>
</tr>
<tr>
<td align="left">无</td>
<td align="left"><strong>updated</strong>：2.0新增的生命周期钩子，在实例挂载之后。</td>
</tr>
<tr>
<td align="left"><strong>beforeDestory</strong>： 在开始销毁实例时调用，此刻实例仍然有效。</td>
<td align="left">没有新增特性</td>
</tr>
<tr>
<td align="left"><strong>destoryed</strong>：在实例被销毁之后调用。此时所有绑定和实例指令都已经解绑，子实例也被销毁。</td>
<td align="left">没有新增特性</td>
</tr>
</tbody></table>
<h2 id="vue挂载的钩子函数"><a href="#vue挂载的钩子函数" class="headerlink" title="vue挂载的钩子函数"></a>vue挂载的钩子函数</h2><p> vue每个组件都是独立的，每个组件都有一个属于它的生命周期，从一个组件创建、数据<strong>创建</strong>、<strong>挂载</strong>、<strong>更新</strong>、<strong>销毁</strong>，<br> 这就是一个组件所谓的生命周期。在组件中常用的方法有:</p>
<h5 id="实例创建之前同步调用：beforeCreate"><a href="#实例创建之前同步调用：beforeCreate" class="headerlink" title="实例创建之前同步调用：beforeCreate"></a>实例创建之前同步调用：beforeCreate</h5><h5 id="实例创建之后调用：created"><a href="#实例创建之后调用：created" class="headerlink" title="实例创建之后调用：created"></a>实例创建之后调用：created</h5><h5 id="实例在挂载之前调用：beforeMount"><a href="#实例在挂载之前调用：beforeMount" class="headerlink" title="实例在挂载之前调用：beforeMount"></a>实例在挂载之前调用：beforeMount</h5><h5 id="实例在挂载之后调用：mounted"><a href="#实例在挂载之后调用：mounted" class="headerlink" title="实例在挂载之后调用：mounted"></a>实例在挂载之后调用：mounted</h5><h5 id="实例修改前嗲用：beforeUpdate"><a href="#实例修改前嗲用：beforeUpdate" class="headerlink" title="实例修改前嗲用：beforeUpdate"></a>实例修改前嗲用：beforeUpdate</h5><h5 id="实例修改后调用：updated"><a href="#实例修改后调用：updated" class="headerlink" title="实例修改后调用：updated"></a>实例修改后调用：updated</h5><h5 id="销毁实例时调用：beforeDestroy"><a href="#销毁实例时调用：beforeDestroy" class="headerlink" title="销毁实例时调用：beforeDestroy"></a>销毁实例时调用：beforeDestroy</h5><h5 id="实例销毁后调用：destroyed"><a href="#实例销毁后调用：destroyed" class="headerlink" title="实例销毁后调用：destroyed"></a>实例销毁后调用：destroyed</h5><hr>
<h2 id="vue路由守卫"><a href="#vue路由守卫" class="headerlink" title="vue路由守卫"></a>vue路由守卫</h2><h3 id="全局路由钩子"><a href="#全局路由钩子" class="headerlink" title="全局路由钩子"></a>全局路由钩子</h3><p><strong>router.beforeEach</strong>：全局前置守卫，当一个导航触发时，全局前置守卫按照创建顺序调用。守卫是异步解析执行。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">router.beforeEach(<span class="function">(<span class="params">to, <span class="keyword">from</span>, next</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;使用beforeEach必须调用next方法&#x27;</span>)</span><br><span class="line">  next()</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<p>to: 将要进入的路由对象<br>from: 当前导航正要离开的路由<br>next: 进入管道中的下一个钩子。如果全部钩子执行完了，就表示当前next状态是确认状态</p>
<hr>
<p><strong>router.afterEach</strong>:全局后置守卫，在所有路由跳转结束的时候调用，这些钩子不会接受next函数也不会对导航本身进行变化</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">router.afterEach(<span class="function">(<span class="params">to, <span class="keyword">from</span>, next</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="comment">//使用beforeEach必须调用next方法</span></span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;无需调用next函数&#x27;</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<p>to: 将要进入的路由对象<br>from: 当前导航正要离开的路由</p>
<h3 id="单个路由钩子"><a href="#单个路由钩子" class="headerlink" title="单个路由钩子"></a>单个路由钩子</h3><p><strong>beforeRouteEnter</strong>:用法与beforeEach类似，必须使用next方法调用</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">components: &#123;</span><br><span class="line">    <span class="function"><span class="title">beforeRouteEnter</span>(<span class="params">to, <span class="keyword">from</span>, next</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;此时无法调用实例&quot;</span>);</span><br><span class="line">    next(<span class="function"><span class="params">sl</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="comment">//因为当钩子执行前，组件实例还没被创建</span></span><br><span class="line">        <span class="comment">// sl 就是当前组件的实例相当于上面的 this，所以在next方法里你就可以把sl当this来用了。</span></span><br><span class="line">        <span class="built_in">console</span>.log(sl);<span class="comment">//当前组件的实例</span></span><br><span class="line">    &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>beforeRouteUpdate</strong>:在当前路由改变，并且该组件被复用时调用，next需要被主动调用，不能传回调。<br>对于一个带有动态参数的路径 /login/:id/，在/login/1/和/login/2/之间跳转的时候,这里只是传递的参数改变</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">beforeRouteUpdate</span>(<span class="params">to, <span class="keyword">from</span>, next</span>)</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;实例被复用&quot;</span>);</span><br><span class="line">    <span class="comment">//必须调用next</span></span><br><span class="line">    next();</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
<p><strong>beforeRouteLeave</strong>:导航离开该组件的对应路由时调用，next需要被主动调用，不能传回调。通常用来禁止用户未保存修改之前离开</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">beforeRouteLeave</span>(<span class="params">to,<span class="keyword">from</span>,next</span>)</span>&#123;</span><br><span class="line">      user = <span class="built_in">window</span>.user</span><br><span class="line">      <span class="keyword">if</span>(user)&#123;</span><br><span class="line">        next()</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">else</span> &#123;</span><br><span class="line">        next(<span class="literal">false</span>)</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">//必须调用next</span></span><br><span class="line">      next()</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://shangguan-lingfeng.gitee.io/blog1/2021/01/26/vue%E9%92%A9%E5%AD%90%E5%87%BD%E6%95%B0/" data-id="cknublgbz0012v8uhdb0eg7ku" data-title="vue钩子函数" class="article-share-link">Teilen</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog1/tags/vue/" rel="tag">vue</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/blog1/2021/01/27/vue%E7%88%B6%E5%AD%90%E7%BB%84%E4%BB%B6/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Neuer</strong>
      <div class="article-nav-title">
        
          vue父子组件
        
      </div>
    </a>
  
  
    <a href="/blog1/2021/01/25/%E5%A6%82%E4%BD%95%E5%AE%9E%E7%8E%B0%E6%94%AF%E4%BB%98%E5%AE%9D%E7%AC%AC%E4%B8%89%E6%96%B9%E6%94%AF%E4%BB%98/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Älter</strong>
      <div class="article-nav-title">vue拦截器</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/CentOS/" rel="tag">CentOS</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/Flask/" rel="tag">Flask</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/django/" rel="tag">django</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/docker/" rel="tag">docker</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/gitee/" rel="tag">gitee</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/linux/" rel="tag">linux</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/mysql/" rel="tag">mysql</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/python/" rel="tag">python</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/python%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/" rel="tag">python基础知识</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/redis/" rel="tag">redis</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/vue/" rel="tag">vue</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/webshell/" rel="tag">webshell</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/%E4%BA%91%E4%B8%BB%E6%9C%BA%E5%AE%9E%E7%8E%B0%E8%A7%86%E9%A2%91%E7%9B%B4%E6%92%AD/" rel="tag">云主机实现视频直播</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/%E5%85%B3%E4%BA%8E%E7%AC%AC%E4%B8%89%E6%96%B9/" rel="tag">关于第三方</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/blog1/tags/CentOS/" style="font-size: 17.5px;">CentOS</a> <a href="/blog1/tags/Flask/" style="font-size: 10px;">Flask</a> <a href="/blog1/tags/django/" style="font-size: 12.5px;">django</a> <a href="/blog1/tags/docker/" style="font-size: 12.5px;">docker</a> <a href="/blog1/tags/gitee/" style="font-size: 10px;">gitee</a> <a href="/blog1/tags/linux/" style="font-size: 10px;">linux</a> <a href="/blog1/tags/mysql/" style="font-size: 12.5px;">mysql</a> <a href="/blog1/tags/python/" style="font-size: 15px;">python</a> <a href="/blog1/tags/python%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/" style="font-size: 17.5px;">python基础知识</a> <a href="/blog1/tags/redis/" style="font-size: 12.5px;">redis</a> <a href="/blog1/tags/vue/" style="font-size: 20px;">vue</a> <a href="/blog1/tags/webshell/" style="font-size: 10px;">webshell</a> <a href="/blog1/tags/%E4%BA%91%E4%B8%BB%E6%9C%BA%E5%AE%9E%E7%8E%B0%E8%A7%86%E9%A2%91%E7%9B%B4%E6%92%AD/" style="font-size: 10px;">云主机实现视频直播</a> <a href="/blog1/tags/%E5%85%B3%E4%BA%8E%E7%AC%AC%E4%B8%89%E6%96%B9/" style="font-size: 12.5px;">关于第三方</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archiv</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/blog1/archives/2021/05/">五月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog1/archives/2021/04/">四月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog1/archives/2021/03/">三月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog1/archives/2021/02/">二月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog1/archives/2021/01/">一月 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">letzter Beitrag</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/blog1/2021/05/14/docker+CentOS+mysql%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8E%E5%90%8C%E6%AD%A5/">docker+CentOS+mysql实现主从同步</a>
          </li>
        
          <li>
            <a href="/blog1/2021/05/13/mysql%E5%AE%9E%E7%8E%B0%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB(%E5%9C%A8%E4%B8%BB%E4%BB%8E%E5%90%8C%E6%AD%A5%E5%9F%BA%E7%A1%80%E4%B8%8A)/">mysql实现读写分离</a>
          </li>
        
          <li>
            <a href="/blog1/2021/05/12/%E5%9C%A8%E9%A1%B9%E7%9B%AE%E4%B8%AD%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E6%8E%A5%E5%8F%A3%E6%96%87%E6%A1%A3/">在项目中自动生成接口文档</a>
          </li>
        
          <li>
            <a href="/blog1/2021/05/12/mysql%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8E%E5%90%8C%E6%AD%A5/">mysql实现主从同步</a>
          </li>
        
          <li>
            <a href="/blog1/2021/05/09/CentOS%E9%83%A8%E7%BD%B2flask%E9%A1%B9%E7%9B%AE/">nginx部署flask项目</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 Jerry<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/blog1/" class="mobile-nav-link">Home</a>
  
    <a href="/blog1/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/blog1/js/jquery-3.4.1.min.js"></script>



  
<script src="/blog1/fancybox/jquery.fancybox.min.js"></script>




<script src="/blog1/js/script.js"></script>





  </div>
</body>
</html>